package com.browsesoft.user;

import java.io.Serializable;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.browsesoft.EntityManager;
import com.browsesoft.ManagerGroup;
import com.browsesoft.Tools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLFormModel;

/**
 * 选择用户所属角色
 */
public class RolesOfUserModel implements HTMLFormModel, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8555469733433138763L;

	/**
	 * 要设置角色的用户
	 */
	private User user = null;

	/**
	 * 初始化model
	 * 
	 * @throws Exception
	 */
	public void init(HttpServletRequest request) throws Exception {
	}

	/**
	 * 设置属性值
	 * 
	 * @param attributes
	 */
	public void updateAttributes(Hashtable attributes) throws Exception {
		// 取得选择的结果
		String names = (String) attributes.get("selectedname");
		if (names == null) {
			return;
		}
		LinkedList selectedRoles = Tools.stringToLinkedList(names);
		// 把结果转换成角色列表
		for (int i = 0; i < selectedRoles.size(); i++) {
			String name = (String) selectedRoles.get(i);
			Role role = (Role) EntityManager.getInstance().getResourceFromName(
					name);
			user.addRole(role);
		}
	}

	/**
	 * 得到属性值
	 * 
	 * @return Hashtable
	 */
	public Hashtable getAttributes() {
		Hashtable hashtable = new Hashtable();
		// 得到用户的所有角色
		LinkedList u = new LinkedList(user.getRoles());
		String uName = "";
		for (int i = 0; i < u.size(); i++) {
			if (i != 0) {
				uName += ',';
			}
			uName += (String) ((Role) u.get(i)).getAttributes().get("name");
		}
		hashtable.put("selectedname", uName);
		return hashtable;
	}

	/**
	 * 取得选项值列表,LinkedList中存储哈西表，select的name和options @
	 */
	public Iterator getOptions(String selectName) throws Exception {
		// 将所有角色列表放入r中
		LinkedList r = new LinkedList();
		r = EntityManager.getInstance().getAllRoles();
		// 将用户拥有的角色放入u中
		LinkedList u = new LinkedList(user.getRoles());
		// 从r中取走u中的内容
		for (int i = 0; i < u.size(); i++) {
			r.remove(u.get(i));
		}
		// 将r中的内容转换成名字
		LinkedList rName = new LinkedList();
		for (int i = 0; i < r.size(); i++) {
			rName.add((String) ((Role) r.get(i)).getAttributes().get("name"));
		}
		// 将u中的内容转换成名字
		LinkedList uName = new LinkedList();
		for (int i = 0; i < u.size(); i++) {
			uName.add((String) ((Role) u.get(i)).getAttributes().get("name"));
		}
		// 根据需要，给出内容
		if (selectName.equals("alignselect")) {
			return rName.iterator();
		} else if (selectName.equals("selected")) {
			return uName.iterator();
		}
		return new LinkedList().iterator();
	}

	/**
	 * 记录客户端环境
	 * 
	 * @param request
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = (String) request.getParameter("userid");
		if (id != null) {
			user = (User) ManagerGroup.getManagerGroup().getObject(id);
		}
	}

	/**
	 * 设置模型所对应的组件
	 * 
	 * @param component
	 *            对应的组件
	 */
	public void setComponent(HTMLBasicComponent component) {
	}

	/**
	 * 模型更新页面元素
	 * 
	 * @throws Exception
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}
}
